/*
    Coder : Dzlua
    Email : 505544956@qq.com
    Time  : 2017/07/04
*/
#include "decoder/decoder.h"
using namespace dzlua;

#include <iostream>
using std::cout;
using std::endl;
using std::cerr;

int main(int argc, char *argv[])
{
    if (argc != 2) {
        cerr << "args err. <filename>" << endl;
        return 1;
    }

    auto dc = av::Decoder::NewInterface();

    bool bok = dc->Init();
    if (!bok) {
        cerr << "init err2." << endl;
        return 2;
    }

    bok = dc->Open(argv[1]);
    if (!bok) {
        cerr << "open err3." << endl;
        return 3;
    }

    // set audio info
    {
        auto info = dc->getAudioInfo();
        av::AudioInfo ainfo;
        ainfo.layout = av::eChannelLayout::stereo;
        ainfo.format = av::eSampleFormat::s16;
        ainfo.rate = 44100;
        ainfo.channels = 2;
        dc->setInfo(&ainfo);
    }

    dc->Run();

    av::sleep_for(1000);
    dc->Pause();
    av::sleep_for(1000);
    dc->Continue();

    dc->WaitDone();

    bok = dc->Open(argv[1]);
    if (!bok) {
        cerr << "open err4." << endl;
        return 4;
    }

    dc->Run();

    av::sleep_for(1000);
    dc->Close();

    bok = dc->Open(argv[1]);
    if (!bok) {
        cerr << "open err5." << endl;
        return 5;
    }

    dc->Run();

    av::sleep_for(1000);
    dc->Stop();
    av::sleep_for(1000);
    bok = dc->Open(argv[1]);
    if (!bok) {
        cerr << "open err6." << endl;
        return 6;
    }
    dc->Run();

    cout << "---------- wait 10s --------------" << endl;
    av::sleep_for(10000);
    bok = dc->Open(argv[1]);
    if (!bok) {
        cerr << "open err7." << endl;
        return 7;
    }
    dc->Run();
    
    av::sleep_for(3000);
    
    return 0;
}
